package cn.wolfcode.web.controller;

import cn.wolfcode.ssm.query.CustomerReportQueryObject;
import cn.wolfcode.ssm.service.ICustomerReportService;
import cn.wolfcode.ssm.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {


    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String List(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        PageInfo<HashMap> pageResult = customerReportService.query(qo);
        model.addAttribute("pageResult",pageResult);

        return "/customerReport/list";
    }


    // 条形图
    @RequestMapping("/bar")
    public String bar(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        // 查询所有的信息不用分组
        List<HashMap> list = customerReportService.selectAll(qo);
        // 准备一个集合存储X轴信息
        ArrayList XList = new ArrayList();

        // 准备一个集合存储Y轴信息
        ArrayList YList = new ArrayList();

        // 遍历集合把对应数据存储到X和Y集合中
        for (HashMap hashMap : list) {
            XList.add(hashMap.get("groupType"));
            YList.add(hashMap.get("number"));
        }

        // 共享到界面上
        model.addAttribute("XList",JSON.toJSONString(XList));
        model.addAttribute("YList",JSON.toJSONString(YList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));

        return "/customerReport/bar";
    }




    // 饼状图
    @RequestMapping("/pie")
    public String pie(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        // 查询所有的信息不用分组
        List<HashMap> data = customerReportService.selectAll(qo);
        // 准备两个个集合
        ArrayList legendList = new ArrayList();
        ArrayList seriesList = new ArrayList();

        // 遍历集合把对应数据存储到X和Y集合中
        for (HashMap hashMap : data) {
            legendList.add(hashMap.get("groupType"));
            // 在准备一个map
            HashMap map = new HashMap();
            // 注意key的名字是固定的
            map.put("name",hashMap.get("groupType"));
            map.put("value",hashMap.get("number"));
            // 在把map装到集合中
            seriesList.add(map);
        }

        // 共享到界面上
        model.addAttribute("legendList",JSON.toJSONString(legendList));
        model.addAttribute("seriesList",JSON.toJSONString(seriesList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));

        return "/customerReport/pie";
    }


}
